import java.util.ArrayList;
import java.util.List;

public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode root;//将来这个引用指向的就是根节点
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }


    public void preOrder(TreeNode root){
        if (root == null){
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //将前序遍历的值存放到List中
    public List<TreeNode> preOrder1(TreeNode root){
        List<TreeNode> ret = new ArrayList<>();
        if (root == null){
            return ret;
        }
        ret.add(root);
        List<TreeNode> leftTree = preOrder1(root.left);
        ret.addAll(leftTree);
        List<TreeNode> rightTree = preOrder1(root.right);
        ret.addAll(rightTree);
        return ret;
    }


    public void inOrder(TreeNode root){
        if (root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    //将中序遍历的值存放到List中
    public List<TreeNode> inOrder1(TreeNode root){
        List<TreeNode> ret = new ArrayList<>();
        if (root == null){
            return ret;
        }
        List<TreeNode> leftTree = inOrder1(root.left);
        ret.addAll(leftTree);
        ret.add(root);
        List<TreeNode> rightTree = inOrder1(root.right);
        ret.addAll(rightTree);
        return ret;
    }


    public void postOrder(TreeNode root){
        if (root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }
    //将后序遍历的值存放到List中
    public List<TreeNode> postOrder1(TreeNode root){
        List<TreeNode> ret = new ArrayList<>();
        if (root == null){
            return ret;
        }
        List<TreeNode> leftTree = postOrder1(root.left);
        ret.addAll(leftTree);
        List<TreeNode> rightTree = postOrder1(root.right);
        ret.addAll(rightTree);
        ret.add(root);
        return ret;
    }

    //获取树中节点的个数
    public int size = 0;
    public int NodeSize(TreeNode root){
        if (root == null){
            return 0;
        }
        size++;
        NodeSize(root.left);
        NodeSize(root.right);
        return size;
    }
    //子问题的思路
    public int NodeSize2(TreeNode root){
        if (root == null) {
            return 0;
        }
        return NodeSize2(root.left)+NodeSize2(root.right) + 1;
    }

    //获取叶子结点的个数
    public int leafSize = 0;
    public void getLeafNodeCount(TreeNode root){
        if (root == null){
            return ;
        }
        if (root.left == null && root.right == null){
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }
    //子问题的思路
    public int getLeafNodeCount2(TreeNode root){
        if (root == null){
            return 0;
        }
        if (root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount2(root.left)+ getLeafNodeCount2(root.right);
    }

    //获取第K层结点的个数,子问题的思路
    public int getKLevelNodeCount(TreeNode root,int k){
        if (root == null){
            return 0;
        }
        if (k == 1){
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }

    //获取二叉树的高度
    public int getHeight(TreeNode root){
        if (root == null){
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
    }
}
